package com.example.springstack.springsecurity.Handler;


import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Qiangfanghao
 */
public class MyAuthenticationException implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        //设置响应状态
        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        //设置返回json字符串
        httpServletResponse.setContentType("application/json;charset=utf-8");
        //写数据
        PrintWriter writer = httpServletResponse.getWriter();
        JSONObject jo = new JSONObject();
        jo.put("status","403");
        jo.put("msg","权限不足");
        writer.write(jo.toJSONString());

        writer.flush();
        writer.close();
    }
}
